//
//  SinaHomeCellViewModel.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaHomeCellViewModel.h"

@implementation SinaHomeCellViewModel



#pragma mark
#pragma mark - 重写pictureModelArray的get方法
-(NSArray<SinaHomePictureModel *> *)pictureUrlInfoModelArray{
    
    if(_homeCellModel.retweeted_status == nil){
        return _homeCellModel.pic_urls;
    }else{
        return _homeCellModel.retweeted_status.pic_urls;
    }
    
}



-(NSURL *)iconURL{
    
    NSString *urlStr = self.homeCellModel.user.avatar_large;
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
    
}

-(UIImage *)rankImage{
    
    NSInteger rank = self.homeCellModel.user.mbrank;
    if(rank > 0 && rank < 7){
        
        NSString *imageName = [NSString stringWithFormat:@"common_icon_membership_level%zd",rank];
        return [UIImage imageNamed:imageName];
        
    }
    return [UIImage imageNamed:@"common_icon_membership_expired"];
    
}


-(UIImage *)vipImage{
    // /// 认证类型: -1：没有认证，0：认证用户，2,3,5: 企业认证，220: 达人
    
    NSInteger vipType = self.homeCellModel.user.verified_type;
    switch (vipType) {
        case -1:
            return nil;
            
        case 0:
            return [UIImage imageNamed:@"avatar_vip"];
            
        case 2:
        case 3:
        case 5:
            return [UIImage imageNamed:@"avatar_enterprise_vip"];
            
        case 220:
            return [UIImage imageNamed:@"avatar_grassroot"];
            
        default:
            return nil;
    }
    
}


//重写日期get方法
-(NSString *)creatDateStr{
    NSString *str = self.homeCellModel.created_at;
    NSString *newStr = [self dealWithDateStr:str];
    return newStr;
}

//重写来源get
-(NSString *)messageSource{
    NSString *source = self.homeCellModel.source;
    NSString *newStr = [self dealWithSourceStr:source];
    return newStr;
}



//处理日期字符串
-(NSString *)dealWithDateStr:(NSString *)dateString{
    //Wed Nov 23 19:30:03 +0800 2016
    NSString *dateStr = self.homeCellModel.created_at;

    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"EEE MMM dd HH:mm:ss z yyyy";
    NSDate *sinaDate = [dateFormatter dateFromString:dateStr];
    if(!sinaDate){
        return @"穿越时空的少年";
    }
    NSDate *currentDate = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *result = [calendar components:NSCalendarUnitYear | NSCalendarUnitDay fromDate:sinaDate toDate:currentDate options:0];
    
    //判断是否是今年
    if(result.year == 0){
        
        //判断是否是今天
        if([calendar isDateInToday:sinaDate]){
            
            NSInteger delta = [currentDate timeIntervalSinceDate:sinaDate];
            if(delta < 3600){
                //一小时之内
                if(delta < 60){
                    //一分钟之内
                    return @"刚刚";
                }else{
                    //一分钟之外
                    NSInteger min = delta / 60;
                    return [NSString stringWithFormat:@"%zd分钟前",min];
                }
            }else{
                //一小时之外
                NSInteger hour = delta / 3600;
                return [NSString stringWithFormat:@"%zd小时前",hour];
            }
            
        }else if([calendar isDateInYesterday:sinaDate]){
            //判断是否是昨天
            return @"昨天";
        }else{
            dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            return [dateFormatter stringFromDate:sinaDate];
        }
        
    }else{
        //不是今年
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        return [dateFormatter stringFromDate:sinaDate];
    }
}


//处理微博来源
-(NSString *)dealWithSourceStr:(NSString *)source{
    ///< href="http://app.weibo.com/t/feed/6vtZb0" rel="nofollow">微博 weibo.com</a>
    
    NSString *beginFlag = @"\">";
    NSString *endFlag = @"</a>";
    
    NSRange beginRange = [source rangeOfString:beginFlag];
    NSRange endRange = [source rangeOfString:endFlag];
    
    NSRange sourceRange = NSMakeRange(beginRange.location + beginRange.length, endRange.location - (beginRange.location + beginRange.length));
    
    NSString *result = [source substringWithRange:sourceRange];
    return result;
}



@end
